import json
import common
import requests
from main import main

def send(
    route,
    json_content=None,
    post=True,
    ip = common.args.ClientUIBackendIP,
    port = common.args.ClientUIBackendPort
):
    url = f"http://{ip}:{port}"
    url = f"{url}{route}"
    if post:
        resp = requests.post(url,json=json_content)
    else:
        resp = requests.get(url)
    resp_content = resp.content.decode("utf-8")
    return resp_content

# OK
r = send("/sign_in",{
    "account_private_key":"1eb8e2955a7c928c1d5b4568c0ea42a3d3f31968f49efc40a1351e3b255f00fe"
})
print(r)

# OK
r = send("/acct_address",post=False)
print(r)


# OK
from binascii import b2a_base64,a2b_base64
from common.strtools import hex_remove_0x
_upload_file = {
    "file_name":b2a_base64(b"hello_world.txt").decode("utf-8"),
    "keywords":[
        b2a_base64(b"1").decode("utf-8"),
        b2a_base64(b"2").decode("utf-8"),
        b2a_base64(b"3").decode("utf-8"),
    ],
    "policy":[
        b2a_base64(b"type1").decode("utf-8"),
        b2a_base64(b"type2").decode("utf-8"),
        b2a_base64(b"type3").decode("utf-8"),
    ],
    "content":b2a_base64(b"hello world").decode("utf-8")
}
r = send("/upload_file",_upload_file)
print(r)

"""
2019-09-11-09-47-23 {'transaction_id': '0xbb51f643e93fc726aaee0632023ab347c2a8466cb311c405b8bad96eb323b19a', 
'file_location': {'Name': 'hello_world.txt', 'Hash': 'QmXsBbXGtZLtho8KNbRfmpFxPb5jbaAtHym2zKXc7qLVDe', 'Size': '72'}}
"""

_download_file = {
    "transaction_id":json.loads(r)['transaction_id'],
}
r = send("/download_file",_download_file)
print(r)
r = json.loads(r)
print(a2b_base64(r["file_name"].encode("utf-8")))
print(a2b_base64(r["content"].encode("utf-8")))